package com.example.wmstore.controller;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wmstore.common.QueryPageParam;
import com.example.wmstore.common.Result;
import com.example.wmstore.entity.Goods;
import com.example.wmstore.entity.Outrecord;
import com.example.wmstore.service.GoodsService;
import com.example.wmstore.service.OutrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TCW
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/outrecord")
public class OutrecordController {
    @Autowired
    private OutrecordService outrecordService;
    @Autowired
    private GoodsService goodsService;
    @GetMapping("/allcount")
    public Result allcount(){
        List<Outrecord> list=outrecordService.list();
        int count=0;
        for(Outrecord outrecord:list)
        {
            if(outrecord.getStatus()=="1"){
                count+=outrecord.getCount();
            }
        }
        return Result.suc(count);
    }
    @PostMapping("/save")
    public Result save(@RequestBody Outrecord outrecord){
        return outrecordService.save(outrecord)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String goods = (String)param.get("goods");
        String userId = (String)param.get("userId");
        String admin_id = (String)param.get("admin_id");
        String status=(String)param.get("status");
        String creat_time=(String)param.get("creat_time");
        Page<Outrecord> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Outrecord> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(goods) && !"null".equals(goods)){
            lambdaQueryWrapper.like(Outrecord::getGoods,goods);
        }
        if(StringUtils.isNotBlank(userId) && !"null".equals(userId)){
            lambdaQueryWrapper.eq(Outrecord::getUserid,userId);
        }
        if(StringUtils.isNotBlank(admin_id) && !"null".equals(admin_id)){
            lambdaQueryWrapper.eq(Outrecord::getAdminId,admin_id);
        }
        if(StringUtils.isNotBlank(status) && !"null".equals(status)){
            lambdaQueryWrapper.eq(Outrecord::getStatus,status);
        }

        if(StringUtils.isNotBlank(creat_time) && !"null".equals(creat_time)){
            lambdaQueryWrapper.eq(Outrecord::getCreateTime,creat_time);
        }
        IPage result = outrecordService.PageList(page,lambdaQueryWrapper);
        for(Object outrecord: result.getRecords()){
            Outrecord outrecord1=(Outrecord) outrecord;
            Goods goods1=goodsService.getById(outrecord1.getGoods());
            outrecord1.setGood(goods1);
        }
        return Result.suc(result.getRecords(),result.getTotal());
    }
}
